package com.qxf.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.qxf.exception.UserConfigurationException;
import com.qxf.model.beans.statistic.AdStatistic;
import com.qxf.model.beans.statistic.MediaStatistic;
import com.qxf.service.jfreechart.PieStatisticData;
import com.qxf.service.jfreechart.StatisticData;
import com.qxf.util.db.SQLProcessor;
import com.qxf.util.db.SQLUtil;

public class StatisticsDao {

	static Logger log = Logger.getLogger(StatisticsDao.class);
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public MediaStatistic getMediaStatistic(String activityId) {
		MediaStatistic mediaStatistic = null;
		String sql = null;
		SQLProcessor sp = null;
		List<StatisticData> mediaList = new ArrayList<StatisticData>();
		List<List<StatisticData>> adList = new ArrayList<List<StatisticData>>();
		PieStatisticData sData = null;
		Map<String, Object> tempMap = null;
		try{
			sp = new SQLProcessor();
			sql = "select a.media_id,m.media_name,a.ad_id,a.ad_name,a.ad_clickcount" +
					" from tb_ad a left join tb_media m on a.media_id=m.media_id" +
					" left join tb_activity t on a.activity_id=t.activity_id" +
					" where a.activity_id=" + SQLUtil.toSqlInt(activityId) +
					" order by a.media_id, a.ad_id";
			List<Map<String, Object>> list = sp.executeQuery(sql);
			if(list.size() == 0) {
				throw new UserConfigurationException("暂无数据");
			}
			
			String mediaName = null;
			String adName = null;
			Integer count = null;
			
			// 统计各媒体总点击数
			log.info("getMediaStatistic(): 计算各媒体点击总数");
			tempMap = new HashMap<String, Object>();
			for(int i=0; i<list.size(); i++){
				sData = new PieStatisticData();
				Map<String, Object> map = list.get(i);
				mediaName = (String)map.get("media_name");
				count = (Integer)map.get("ad_clickcount");
				sData.setChartName("媒体统计");
				sData.setName(mediaName);
				if(tempMap.containsKey(mediaName)) {
					sData.setCount(((PieStatisticData)tempMap.get(mediaName)).getCount() + count);
					tempMap.put(mediaName, sData);
				} else {
					sData.setCount(count);
					tempMap.put(mediaName, sData);
				}
			}
			Iterator mediaIt = tempMap.values().iterator();
			while(mediaIt.hasNext()) {
				sData = (PieStatisticData)mediaIt.next();
				mediaList.add(sData);
			}
			// 统计各媒体广告点击
			log.info("getMediaStatistic(): 计算各媒体广告位点击总数");
			tempMap = new HashMap<String, Object>();
			List<PieStatisticData> tempAdList = null;
			for(int i=0; i<list.size(); i++){
				sData = new PieStatisticData();
				Map<String, Object> map = list.get(i);
				mediaName = (String)map.get("media_name");
				adName = (String)map.get("ad_name");
				count = (Integer)map.get("ad_clickcount");
				sData.setChartName(mediaName);
				sData.setName(adName);
				sData.setCount(count);
				if(tempMap.containsKey(mediaName)) {
					tempAdList = (List)tempMap.get(mediaName);
					tempAdList.add(sData);
					tempMap.put(mediaName, tempAdList);
				} else {
					tempAdList = new ArrayList<PieStatisticData>();
					tempAdList.add(sData);
					tempMap.put(mediaName, tempAdList);
				}
			}
			Iterator adIt = tempMap.values().iterator();
			while(adIt.hasNext()) {
				adList.add((List)adIt.next());
			}
			
			mediaStatistic = new MediaStatistic();
			mediaStatistic.settData(mediaList);
			mediaStatistic.setAdList(adList);
			log.info("SQL: " + sql);
		} catch(SQLException se){
			log.info("Error SQL: " + sql);
			se.printStackTrace();
		} catch (UserConfigurationException e) {
			log.error("getMediaStatistic(): " + e.getMessage());
		} finally{
			SQLProcessor.staticClose(sp);
		}
		return mediaStatistic;
	}
	
	public AdStatistic getAdStatistic(String activityId) {
		return null;
	}
}
